<?php
/**
 * 日程控制类
 * @author liubin-bt
 * @property Schedule_model $Schedule_model
 * @property ScheduleUser_model $ScheduleUser_model
 * @property User_model $User_model
 * @property Project_model $Project_model
 */
class Schedule extends Base{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('Schedule_model');
    }
    
    public function myList() {
        $date = $this->getSafeRequest('date', '');
        $type = $this->getSafeRequest('type', '');
        if( !$date ){
            $this->outJsonData(-1, '日期不能为空');
        }
        $project_id = intval($this->getSafeRequest('project_id', 0));
        $list = $this->Schedule_model->getList($this->uid, $project_id, $date);
        $this->load->model('ScheduleUser_model');
        $schedule_ids = array_column($list, 'id');
        $schedule_users = $this->ScheduleUser_model->getUserList($schedule_ids);
        foreach($list as $k=>&$v){
            if( $type ){
                //没有用户
                if( !isset($schedule_users[$v['id']]) ){
                    unset($list[$k]);
                    continue;
                }
                //不在日程里
                $susers = array_column($schedule_users[$v['id']], 'uid');
                if(!in_array($this->uid, $susers)){
                    unset($list[$k]);
                    continue;
                }
            }
            $v['users'] = isset($schedule_users[$v['id']]) ? $schedule_users[$v['id']] : [];
            $v['start'] = date('Y-m-d H:i:s', $v['start']);
            $v['end'] = date('Y-m-d H:i:s', $v['end']);
        }
        sort($list);
        $this->outJsonData(0, $list);
    }
    
    public function detail(){
        $id = intval($this->getSafeRequest('id', 0));
        if( !$id ){
            $this->outJsonData(-1, 'id不能为空');
        }
        $data = $this->Schedule_model->get($id);
        if( !$data ){
            $this->outJsonData(-1, '该日程不存在');
        }
        $this->load->model('ScheduleUser_model');
        $this->load->model('User_model');
        $users = [];
        //users获取
        $schedule_users = $this->ScheduleUser_model->getUserList([$id]);
        $data['project_name'] = '';
        if( !empty($data['project_id']) ){
            $this->load->model('Project_model');
            $project = $this->Project_model->getProjectOne($data['project_id'], 'project_name');
            $data['project_name'] = isset($project['project_name']) ? $project['project_name'] : '';
        }
        $data['start'] = date('Y-m-d H:i:s', $data['start']);
        $data['end'] = date('Y-m-d H:i:s', $data['end']);
        $data['users'] = isset($schedule_users[$id]) ? $schedule_users[$id] : [];
        $data['isjoin'] = 0;
        if( $data['users'] ){
            foreach ($data['users'] as $v){
                if( $this->uid == $v['uid'] ){
                    $data['isjoin'] = 1;
                    $data['joinstatus'] = $v['status'];
                }
            }
        }
        $this->outJsonData(0, $data);
    }
    
    /**
     * 修改个人日程状态
     */
    public function joinStatus(){
        $this->load->model('ScheduleUser_model');
        $schedule_id = intval($this->getSafeRequest('id', ''));
        $status = intval($this->getSafeRequest('status', 0));
        if( !$schedule_id ){
            $this->outJsonData(-1, '日程id不能为空');
        }
        if( !in_array($status, [1, 2, 3]) ){
            $this->outJsonData(-1, 'status值非法');
        }
        $map['uid'] = $this->uid;
        $map['schedule_id'] = $schedule_id;
        $res = $this->ScheduleUser_model->update($map, ['status'=>$status]);
        $this->outJsonData(0, '修改成功');
    }
}
?>